package com.survey.application.dao;

import java.lang.reflect.ParameterizedType;






import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.hibernate.SessionFactory;


/**
 * 抽象的dao实现，专门用于继承
 * @author Sunxg
 *
 * @param <T>
 */
public abstract class AbstractBaseDao<T> implements InterBaseDao<T>{
	@Resource
	private SessionFactory sf;
	//通过反射动态获取clazz
	private Class clazz;
	@SuppressWarnings("unchecked")
	public AbstractBaseDao(){
		//获得泛型类型
		ParameterizedType type=(ParameterizedType) this.getClass().getGenericSuperclass();
		clazz=(Class<T>)type.getActualTypeArguments()[0];
	}
	@Override
	public void saveBean(T bean) {
		sf.getCurrentSession().save(bean);
	}

	@Override
	public void updateBean(T bean) {
		sf.getCurrentSession().update(bean);
	}

	@Override
	public void deleteBean(T bean) {
		sf.getCurrentSession().delete(bean);
	}

	@Override
	public T getBean(String id) {
		return (T) sf.getCurrentSession().get(clazz, id);
	}
	@SuppressWarnings("unchecked")
	public List<T> getBeanByHql(String hql){
		return (List<T>) sf.getCurrentSession().createQuery(hql).list();
	}


}

